#include <iostream>
#include <cstdlib>
#include <valarray>
#include <array>
#include <algorithm>

using namespace std;

template <typename Type>
Type refe(const Type a,  const Type b);
void quest_1();
void quest_2();
void quest_3();
void quest_4();
void quest_5();
void quest_6();

int main()
{
    quest_6();

	return 0;
}

template <typename Type>
Type refe(const Type a, const Type b) 
{
	Type x = a * b;
	return x;
}

void quest_1()
{
    /* quest 1 */
	int ix, iy;
	cout << "Enter two data: ";
	cin >> ix >> iy;
	int a = refe(ix, iy);
	cout << "the const return reference: " << a << endl;
}

void quest_2()
{
	int a[10] = {1, 2, 3, 4, 5, 2, 4, 6, 8, 9};
	valarray<int> buffer(a, 10);
    int cnt = 0, ix;

	for (ix = 0; ix < buffer.size(); ix++) 
	   cout << buffer[ix] << ' ';
	cout << endl;

    int value;
	cout << "NUm2:\tthe value want find\n";
	cin >> value;
	for (ix = 0; ix < buffer.size(); ix ++) {
		if (buffer[ix] == value)
		   cnt ++;
		else 
		    continue;
	}
	cout << "The value " << value 
	     << " num. is " << cnt << endl;
}

/* g++ -std=c++11 .\reference.cpp */

void quest_3()
{

	array<int, 10> buffer = {1, 2, 3, 4, 5, 2, 4, 6, 8, 9};
    int cnt = 0, ix, iy;

	for (ix = 0; ix < buffer.size(); ix++) 
	   cout << buffer[ix] << ' ';
	cout << endl;

    int value;
	cout << "Num:\tthe value want find\n";
	cin >> value;
	for (ix = 0; ix < buffer.size(); ix ++) {
		if (buffer[ix] == value)
		   cnt ++;
		else 
		    continue;
	}
	cout << "The value " << value 
	     << " num. is " << cnt << endl;

	array<array<int, 3>, 4> a = {1, 2, 3, 4, 5, 2, 4, 6, 8, 9, 13, 5};	
	for (size_t i = 0; i < a.size(); i++ ){
		for (size_t y = 0; y < a[i].size(); y++) {
			cout.width(4);
            cout << a[i][y];
		}
		cout << endl;   
	} 
}


int factorial(int n)
{
    if (n <= 1)
	    return 1;
	else 
	    return n * factorial(n - 1);
}

void quest_4()
{
	long n, fac;

	cout << "Enter the number you want to calculate: ";
	cin >> n;
	fac = factorial(n);

	cout << "The factorial number is: " << fac << endl;
}

template<class T>
int sum(T &a, int n)
{
    if( n > 0)
	   return sum(a, n - 1) + a[n - 1];
    else 
	    return 0;
}

void quest_5()
{
    vector<int> a;
	int n, rest;
	
	cout << "Enter a number that you want to produce: ";
	cin >> n;

	for (int ix = 0; ix < n; ix ++) {
		a.push_back(rand()%100);
	}
    
	for (int iy = 0; iy < a.size(); iy ++) {
		cout << a[iy] << ' ';
		if (iy % 10 == 9)
		   cout << endl;
	}
	rest = sum(a, n);
    
	cout << "the sum is " << rest << endl; 
}

template<class T>
T sum(T *a, int n)
{
    T sum = 1;
	return accumulate(a, a + n, sum, multiplies<T>());
}

void quest_6()
{
	int a[10] = {0}, n, rest;
    cout << "Enter a number that you want to produce: ";
	cin >> n;

	for (int ix = 0; ix < n; ix ++) {
		a[ix] = rand()%100;
	}

	for (int iy = 0; iy < n; iy ++) {
		cout << a[iy] << ' ';
		if (iy % 10 == 9)
		   cout << endl;
	}
	rest = sum(a, n);
    
	cout << "the sum is " << rest << endl;
}